// 导入数据库操作模块
const { query } = require('express');
const db = require('../../db/index.js');
// TODO: 新增发布新文章
exports.addArticle = (req,res) => {
    // 定义查询分类列表SQL语句-核心
    const sql = 'INSERT INTO ev_artices SET?';
    console.log(req.body);
    db.query(sql,req.body,(err,results)=> {
        if(err)return res._text(err);
        if(results.affectedRows !== 1)return res._text('新增文章失败');
        res._text('新增文章成功',0);
    });
};
// TODO: 获取文章列表数据
exports.listArticle = (req,res) => {
    const sql = 'SELECT * FROM ev_artices WHERE is_delete=0 ORDER BY id ASC';
    db.query(sql,(err,results) => {
        if(err)return res._text(err);
        console.log(results);
        res.send({
            code: 200,
            status: 0,
            message: '获取文章列表成功',
            data: results
        });
    });
};
// TODO: 根据id删除文章数据
exports.deldeteArticleById = (req,res) => {
    // 定义标记删除的SQL语句-核心
    const sql = 'UPDATE ev_artices SET is_delete=1 WHERE id=?';
    db.query(sql,req.body.Id,(err,results) => {
        if(err)return res._text(err);
        console.log(results);
        if(results.affectedRows !== 1)return res._text('删除文章失败');
        res._text('删除成功',0);
    });
};
// TODO: 根据id获取文章详情
exports.queryArticleById = (req,res) => {
    const sql = 'SELECT * FROM ev_artices WHERE is_delete=0  AND id=?';
    db.query(sql,req.body.Id,(err,results) => {
        if(err)return res._text(err);
        if(results.length !== 1)return res._text('查询不到文章详情数据')
        res.send({
            code: 200,
            status: 0,
            message: '查询成功',
            data: results[0]
        });
    });
};
// TODO: 根据id更新文章信息
exports.updateArticleById = (req,res) => {
    const sqlSrc = 'SELECT * FROM ev_artices WHERE is_delete=0  AND id=?';
    db.query(sqlSrc,req.body.Id,(err,results) => {
        if(err)return res._text(err);
        if(results.length !== 1)return res._text('查询不到数据');
        const sql = 'UPDATE ev_artices SET? WHERE id=?';
        db.query(sql,[req.body,req.body.Id],(err,results) => {
            if(err)return res._text(err);
            if(results.affectedRows !== 1)return res._text('修改文章数据失败');
            res._text('修改文章数据成功',0);
        });
    });
};